﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHModel.Entities;
using System.Web;

namespace NHBLL
{
    public static class TeacherSubjectInfoService
    {

        public static IList<TeacherSubjectInfo> GetAll()
            {

                //HttpContext context = HttpContext.Current;
                //ISession urrentSession = context.Items[CurrentSessionKey] as ISession;
                //if (currentSession == null)
                //{
                //    currentSession = sessionFactory.OpenSession();
                //    context.Items[CurrentSessionKey] = currentSession;
                //}
                ISession session = NHelper.GetCurrentSession();

                IQuery iq = session.CreateQuery("from TeacherSubjectInfo");
                return iq.List<TeacherSubjectInfo>();
            }

        public static bool Add(TeacherSubjectInfo u)
            {
                ISession session = NHelper.GetCurrentSession();
                ITransaction it = session.BeginTransaction();
                bool flag = true;
                try
                {
                    session.Save(u);
                    it.Commit();
                }
                catch (Exception ex)
                {
                    flag = false;
                    it.Rollback();
                    throw (ex);
                }
                return flag;
            }

            public static bool Update(object obj)
            {
                bool flag = true;
                ISession session = NHelper.GetCurrentSession();

                ITransaction it = session.BeginTransaction();
                try
                {
                    session.Update(obj);
                    it.Commit();
                }
                catch (Exception)
                {
                    flag = false;
                    it.Rollback();
                    throw;
                }
                session.Flush();
                return flag;
            }

            public static bool Delete()
            {
                bool flag = true;
                ISession session = NHelper.GetCurrentSession();
                ITransaction it = session.BeginTransaction();
                try
                {
                    IQuery iq = session.CreateSQLQuery("delete from TeacherSubjectInfo where UserName=:a");
                    iq.SetParameter("a", "Andy");
                    iq.ExecuteUpdate();
                    it.Commit();
                }
                catch (Exception)
                {
                    flag = false;
                    it.Rollback();
                    throw;
                }
                session.Flush();
                return flag;
            }
        
    }
}
